﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace Helper.IO
{
    public class Path
    {
        static Path _path;
        public static Path Instance
        {
            get
            {
                if (_path == null)
                {
                    _path = new Path();
                }
                return _path;
            }
        }
        /// <summary>
        /// 获取相对路径
        /// </summary>
        /// <param name="RelativePath">用来比较的路径</param>
        /// <param name="OriginlPath">原始路径</param>
        /// <returns></returns>
        public string GetRelativePath(string RelativePath, string OriginlPath)
        {
            if (String.IsNullOrEmpty(RelativePath) || String.IsNullOrEmpty(OriginlPath))
            {
                return "";
            }

            if (System.IO.Path.GetPathRoot(RelativePath) != System.IO.Path.GetPathRoot(OriginlPath))
            {
                return OriginlPath;
            }
            string newPath = "";
            string[] temp1 = RelativePath.Split("\\".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
            string[] temp2 = OriginlPath.Split("\\".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
            int num = temp1.Length < temp2.Length ? temp2.Length : temp1.Length;
            string temp = "";
            for (int i = 0; i < num; i++)
            {
                if (i < temp1.Length && i < temp2.Length)
                {
                    if (String.Compare(temp1[i], temp2[i], true) != 0)
                    {
                        newPath += @"\..";
                    }
                }
                else
                {
                    if (temp1.Length > temp2.Length)
                    {
                        temp = temp1[i];
                    }
                    else
                    {
                        temp = temp2[i];
                    }
                    //if (i == num - 1)
                    //{
                    //    newPath += temp;
                    //}
                    //else
                    //{
                    //    newPath += temp + @"\";
                    //}

                    newPath += @"\" + temp;
                }


            }

            return newPath;
        }
        /// <summary>
        /// 文件重命名
        /// </summary>
        /// <param name="OriginlPath">源文件路径</param>
        /// <param name="NewFileName">新文件名</param>
        /// <returns></returns>
        public string ReName(string OriginlPath, string NewFileName)
        {
            string emailPath =System.IO.Path.GetDirectoryName(OriginlPath) + "\\" + NewFileName;
            FileInfo file = new FileInfo(OriginlPath);
            if (File.Exists(emailPath))
            {
                File.Delete(emailPath);
            }
            file.MoveTo(emailPath);
            return emailPath;
        }
    }
}
